#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "./lcd/bsp_ili9341_lcd.h"
#include "usart.h"
#include "key.h"
#include "stdio.h"
#include "queue.h"
#include "semphr.h"
#include "limits.h"

#define KEY1_EVENT (0x01 << 0)
#define KEY2_EVENT (0x02 << 1)

static TaskHandle_t AppTaskCreate_Handle = NULL;
static TaskHandle_t led_task_handle = NULL;
static TaskHandle_t key_task_handle = NULL;


static void AppTaskCreate(void);
static void LED_Task(void* pvParameters);
static void KEY_Task(void* pvParameters);

SemaphoreHandle_t count_sem_handle; 

int main(void)
{ 
	BaseType_t xReturn = pdPASS;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	KEY_Init();
	LED_Init();		        //初始化LED端口
	ILI9341_Init();//液晶初始化    
	LCD_SetColors(BLACK,WHITE);//设置白底黑字
	LCD_SetFont(&Font8x16);//设置字体大小
	ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);//清屏
  ILI9341_GramScan ( 6 );//设置显示模式
	USART_Config(115200);
  LED_Color(LED_OFF);
	
	xReturn = xTaskCreate((TaskFunction_t )AppTaskCreate,  
                        (const char*    )"AppTaskCreate",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )1, 
                        (TaskHandle_t*  )&AppTaskCreate_Handle);
	if(pdPASS == xReturn){
		vTaskStartScheduler();          //开启任务调度
	}
  else{
		printf("Error Start RTOS\n");
	}
	
}


static void AppTaskCreate(void)
{
  BaseType_t xReturn = pdPASS;
  
  taskENTER_CRITICAL();   
    
	xReturn = xTaskCreate((TaskFunction_t )LED_Task,  
                        (const char*    )"LED_Task",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )2, 
                        (TaskHandle_t*  )&led_task_handle);
  if(xReturn != NULL)
	{
		printf("Success Create Take_Task\r\n");
	}
  
	xReturn = xTaskCreate((TaskFunction_t )KEY_Task,  
                        (const char*    )"KEY_Task",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )3, 
                        (TaskHandle_t*  )&key_task_handle);
  if(xReturn != NULL)
	{
		printf("Success Create Give_Task\r\n");
	}
	
  vTaskDelete(AppTaskCreate_Handle);
  taskEXIT_CRITICAL();          
}


static void LED_Task(void* pvParameters)
{
	BaseType_t xReturn = pdTRUE;
	uint32_t r_event = 0;
	uint32_t last_event = 0;
	uint32_t flag = 0;
	while(1)
	{
		xReturn = xTaskNotifyWait(0x0,ULONG_MAX,&r_event,portMAX_DELAY);
		if(pdTRUE == xReturn)
		{
			last_event |= r_event;
			if(last_event == (KEY1_EVENT | KEY2_EVENT))
			{
				last_event = 0;
				LED_Color( (flag++)%8 );
			}
			else
			{
				last_event = r_event;
			}
		}	
	}
}
static void KEY_Task(void* parameter)
{	
  BaseType_t xReturn = pdTRUE;
  while (1)
  {
		if(GET_KEY1){
			xTaskNotify(led_task_handle,KEY1_EVENT,eSetBits);
		}
		if(GET_KEY2){
			xTaskNotify(led_task_handle,KEY2_EVENT,eSetBits);
		}
		vTaskDelay(20); 
  }
}
